package com.yan.yjhTest.concurrency.simpleDateFormat;

import java.text.SimpleDateFormat;

/**
 * simpleDateFormat线程不安全演示
 */
public class Test {

    // 一般我们使用SimpleDateFormat的时候会把它定义为一个静态变量，避免频繁创建它的对象实例
    private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY-MM-dd");

    /**
     * 线程不安全
     * @param args
     */
    /*public static void main(String[] args) {

        String[] dateStringArray = new String[] { "2020-09-10", "2020-09-11", "2020-09-12", "2020-09-13", "2020-09-14"};

        NotSafeThread[] notSafeThreads = new NotSafeThread[5];

        // 创建线程
        for (int i = 0; i < 5; i++) {
            notSafeThreads[i] = new NotSafeThread(simpleDateFormat, dateStringArray[i]);
        }

        // 启动线程
        for (int i = 0; i < 5; i++) {
            notSafeThreads[i].start();
        }

    }*/

    /**
     * 线程安全
     * @param args
     */
    public static void main(String[] args) {
        String[] dateStringArray = new String[] { "2020-09-10", "2020-09-11", "2020-09-12", "2020-09-13", "2020-09-14"};

        SafeDateThread[] safeDateThread = new SafeDateThread[5];

        // 创建线程
        for (int i = 0; i < 5; i++) {
            safeDateThread[i] = new SafeDateThread(dateStringArray[i]);
        }

        // 启动线程
        for (int i = 0; i < 5; i++) {
            safeDateThread[i].start();
        }
    }

}
